package ynu.edu.online_course.mapper;

import org.apache.ibatis.annotations.*;
import ynu.edu.online_course.entity.UserExtension;

@Mapper
public interface UserExtensionMapper {

    // 根据用户ID查询用户扩展信息
    @Select("SELECT * FROM user_extension WHERE user_id = #{userId}")
    @Results({
            @Result(column = "user_id", property = "user.id"),
            @Result(column = "total_watch_time", property = "totalWatchTime")
    })
    UserExtension findByUserId(@Param("userId") Long userId);

    // 插入用户扩展信息
    @Insert("INSERT INTO user_extension (user_id, total_watch_time) " +
            "VALUES (#{user.id}, #{totalWatchTime})")
    int insertUserExtension(UserExtension userExtension);

    // 更新用户扩展信息
    @Update("UPDATE user_extension SET total_watch_time = #{totalWatchTime} WHERE user_id = #{user.id}")
    int updateUserExtension(UserExtension userExtension);

    // 删除用户扩展信息
    @Delete("DELETE FROM user_extension WHERE user_id = #{userId}")
    int deleteUserExtension(@Param("userId") Long userId);


    @Update("UPDATE user_extension SET manage_course_count = manage_course_count + 1 WHERE user_id = #{userId}")
    void incrementCourseCount(@Param("userId") Long userId);

    @Update("UPDATE user_extension SET manage_course_count = manage_course_count - 1 WHERE user_id = #{userId}")
    void decrementCourseCount(@Param("userId") Long userId);
}
